
$(document).ready(function(){
	var HtmlSli = "<input type='button' style='cursor: pointer;'value=''/>";
	var page_D;//分页数
	var King_data = null;//分类标签内容
	var kind_search = 0;//点击类型，是搜索框还是点击标签搜索
	function page(page_D,now_curr){
		layui.use(['laypage', 'layer'], function(){
		 var laypage = layui.laypage
  		,layer = layui.layer;

			laypage.render({
			    elem: 'demo0'
			    ,count:page_D
			    ,limit:25
			   	,skip:true
			     ,curr:now_curr
			    ,jump: function(obj, first){
			      if(!first){
			        $('.buser_list').find("ul").html(' ');
				       if(kind_search ==0){
				       	get_listData2(obj.curr);
				       }else{
				       	var Sea_val = $(".sea_Text").val();
				       	get_listData(obj.curr,Sea_val);
				       }
			       
			      
			      		}
			   		 }
			 	 });
		
		});
	}
	
	
	///
	$('.dw_name').text($.cookie('Name'));
    $('.dw_part').text($.cookie('Department'));
   ////分类项初始化
   
     $.ajax({
			type:"get",
			url:"http://www.duoweish.com/apis/CstmKind/",
			data:{
				UserId:$.cookie('ID')
				},			
			success:function(data){
				for (var i = 1;i<data.length;i++) {
					
					$(".buser_kind").append(HtmlSli);
					$(".buser_kind").find("input").eq(i).val(data[i]);
				}
				kind_click();
			}
		});	
		
		var selectItem = [[],[],[],[],[],[],[]];
		$(".select-item").find("label").click(function(){
			var Index = Number($(this).parent().parent().attr("index"));
			if($(this).attr("class")==undefined||$(this).attr("class")==""){
				
				selectItem[Index].push($(this).text());
			
				$(this).css("color","white")
				$(this).css("background","#78c1e7")
				$(this).attr("class","close-l")
			}else{
				for (var i = 0; i<selectItem[Index].length;i++) {
					if(selectItem[Index][i]==$(this).text()){
						selectItem[Index].splice(i,1)
					}
				}
				$(this).css("color","black")
				$(this).css("background","white")
				$(this).attr("class","")
			}
			
			
		})
		
	//分类列表按钮点击搜索
	$(".certainBtn").click(function(){
			$(".buser_list").find("ul").html(" ");
			get_listData2(1);
		    page(page_D,1);
		})
		//分类点击
	function kind_click(){
		$(".select").find("input").click(function(){
				$(".select").find("input").removeClass("allItem");
				$(this).addClass('allItem');
				page_change=1;
					$(".buser_list").find("ul").html(" ");
				 	King_data =  $(this).val();
				//Ajax();
				//page();
				get_listData2(1);
				page(page_D,1)
				})
	}
	$(".sea_Btn").click(function(){
		  		var Sea_val = $(".sea_Text").val();
		  		get_listData(1,Sea_val);
		  		
		  				
			})
	///搜索框搜索
	function get_listData (curr,Sea_val){
			$.ajax({
		  			headers:{
		  				Accept:"application/json;charset=utf-8"
		  			},
		  			type:"get",
		  			url:"http://www.duoweish.com/apis/CstmManageSearch/",
		  			data:{
		  				Content:Sea_val,
		  				page:curr||1,
		  				rows:24,
		  				UserId:$.cookie('ID')
		  			},
		  			success:function(data){
		  				if(data == "failure"){
						layui.use(['layer', 'form'], function(){
  								layer.msg('未找到指定商家',{
  									time:1000,
  								    end: function (index) {
    									}
  								});
  							
						});		  					
		  				}else{
		  				kind_search = 1;
		  				page_D = data[1];
		  				page(page_D,curr);
		  				$(".buser_list").find("ul").html(" ");
		  				List(data);	
		  				}
		  			}
		  		})
		}
	//初始化列表
	get_listData2(1);
	
	////查询
	function get_listData2(curr){
				
				for (var i = 0; i<selectItem.length;i++) {
					if(selectItem[i]==""){
						selectItem[i].push("None")
					}
				}
				if(King_data==null){
					King_data = "全部"
				}
				console.log(King_data)
				//
				$.ajax({
			    headers:{
			    	Accept:"application/json;charset=utf-8"
			    },
				type:"get",
				url:"http://www.duoweish.com/apis/CstmManage/",
				data:{
						page:curr||1,
						rows:24,
						Kind:King_data,
						Credit:selectItem[0].join("-"),
						Nature:selectItem[1].join("-"),
						Channel:selectItem[2].join("-"),
						StoreKind:selectItem[3].join("-"),
						SaleKind:selectItem[4].join("-"),
						Month:selectItem[5].join("-"),
						Price:selectItem[6].join("-"),
						UserId:$.cookie('ID')
				},
				success:function(data){
					for (var i = 0; i<selectItem.length;i++) {
						if(selectItem[i][0]=="None"){
							selectItem[i]=[];
						}
				    }
					king_search = 0;
					page_D=data[1];
					page(page_D,curr,king_search);
					 List(data);
					}
	
				})
			}
	
	function List(data){
	       if(data[0].length == 0){
					$(".buser_list").find("ul").html("<span style='font-size: 22px;color:gray;'>没有找到数据~~</span>")
				}
	             
				var Length = data[0].length;
				for(var i =0 ;i<Length;i++){
							var Img 
					         if(data[0][i][3]!=''){
					         	Img = `<img src="${data[0][i][3]}" />`;
					         }else{
					         	Img = `<img src="img/avatar2.jpg" />`;
					         }
							if(data[0][i][5] == 'new'){
								var Htmlis = `<li class="col-lg-2 col-sm-6">
												<div  class="buser_list_item">
												   <a class='ui blue right corner label'>
												       <div style='margin-left:10px;margin-top:6px;'>New</div>
												   </a>
												   <span class="sing_box"></span>
												   <div class="buser_img">${Img}</div>
												   
								        			<p class="buser_name">${data[0][i][0]}</p>
								        			<p class="buser_info"><span>销量：</span><span>${data[0][i][1]}</span></p>
								        			<p class="buser_info"><span>信用：</span><span>${data[0][i][2]}</span></p>
								        			<div><a class="buser_detail" urlid="${data[0][i][4]}">点击查看详情</a></div>
												</div>
								          </li>`;
							}
							else if(data[0][i][5] == 'top'){
								var Htmlis = `<li class="col-lg-2 col-sm-6">
												<div  class="buser_list_item">
												   <a class='ui red right corner label sign_bg'>
												    <div style='margin-left:10px;margin-top:6px;'>Top</div>
												   </a>
												   <span class="sing_box"></span>
												   <div class="buser_img">${Img}</div>
												   
								        			<p class="buser_name">${data[0][i][0]}</p>
								        			<p class="buser_info"><span>销量：</span><span>${data[0][i][1]}</span></p>
								        			<p class="buser_info"><span>信用：</span><span>${data[0][i][2]}</span></p>
								        			<div><a class="buser_detail" urlid="${data[0][i][4]}">点击查看详情</a></div>
												</div>
												
											</li>`;
							}
							else{
								var Htmlis = `<li class="col-lg-2 col-sm-6">
												<div  class="buser_list_item">
												    <div class="buser_img">${Img}</div>
												   
								        			<p class="buser_name">${data[0][i][0]}</p>
								        			<p class="buser_info"><span>销量：</span><span>${data[0][i][1]}</span></p>
								        			<p class="buser_info"><span>信用：</span><span>${data[0][i][2]}</span></p>
								        			<div><a class="buser_detail" urlid="${data[0][i][4]}">点击查看详情</a></div>
												</div>
										    </li>`;
								
							}
						$(".buser_list").find("ul").append(Htmlis);
					}
				
		
				look_details();
				}
	
	function look_details(){
		$(".buser_detail").click(function(){
		
			var ID = $(this).attr("urlid");
			console.log(ID)
			 layer.open({
						        type: 2
						        ,title: false
						        ,closeBtn: 1
						        ,area:['100%','100%']
						        ,shade: 0
						        ,id: 'LAY_layuipro' 
						        ,shadeClose:true
						        ,btnAlign: 'c'
						        ,moveType: 1 
						        ,content:'http://www.duoweish.com/crm/user_infor.html?Id_data='+ID
						        ,success:function(index){
						        	var close = layer.getChildFrame('body',index);
						        	var clo_btn = close.find('.return_icon');
						        	clo_btn.click(function(){
						        		layer.close(index);
						        	})
						        }
							   			  
					         })
					 $(".content-wrapper").hide();
					 $(".layui-layer-iframe").addClass("user_infor_iframe");
					})
	               }
	

			 layer.open({
						        type: 2
						        ,title: false
						        ,closeBtn: 1
						        ,area:['25%','100%']
						        ,offset:['0','100%']
						        ,shade: 0
						        ,id: 'LAY_layuipro2' 
						        ,shadeClose:true
						        ,btnAlign: 'c'
						        ,moveType: 1 
						        ,content:'http://www.duoweish.com/crm/new_buser.html?'
							   	,success:function(){
							   		
							   	}
					        })
			 
			// $(".layui-layer-iframe").hide();
			 $(".new_creat").click(function(){
			 	$(".layui-layer-iframe").show();
			 	$(".layui-layer-iframe").addClass('close_iframe');
			 	$(".layui-layer-iframe").animate({left:'75%'})
			 	
			 })
		
})
